package com.itboye.ihomebank.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.ttKey.ViewHolder;
import com.ttlock.bl.sdk.scanner.ExtendedBluetoothDevice;

/**
 * 发现设备
 * 
 */
public class SheBeiAdapter extends BaseAdapter {
	private Context mContext;
	private List<ExtendedBluetoothDevice> mLeDevices;

	public SheBeiAdapter(Context context,
			List<ExtendedBluetoothDevice> mLeDevices) {
		mContext = context;
		this.mLeDevices = mLeDevices;
	}

	/**
	 * 更新扫描结果
	 * 
	 * @param extendedBluetoothDevice
	 */
	public void updateDevice(ExtendedBluetoothDevice extendedBluetoothDevice) {
		boolean contain = false;
		boolean update = false;
		for (ExtendedBluetoothDevice device : mLeDevices) {
			if (device.equals(extendedBluetoothDevice)) {
				contain = true;
				if (device.isSettingMode() != extendedBluetoothDevice
						.isSettingMode()) {
					device.setSettingMode(extendedBluetoothDevice
							.isSettingMode());
					update = true;
				}
			}
		}
		if (!contain) {
			mLeDevices.add(extendedBluetoothDevice);
			update = true;
		}
		if (update)
			notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mLeDevices.size();
	}

	@Override
	public Object getItem(int position) {
		return mLeDevices.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public boolean isEnabled(int position) {
		if (((ExtendedBluetoothDevice) getItem(position)).isSettingMode())
			return true;
		return false;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = ViewHolder.get(mContext, convertView,
				R.layout.item_she_bei);
		TextView deviceName = viewHolder.getView(R.id.shebei_title);
		TextView macAddress = viewHolder.getView(R.id.shebei_leibie);
		TextView addIcon = viewHolder.getView(R.id.shebei_add);
		if (mLeDevices.get(position).isSettingMode())
			addIcon.setVisibility(View.VISIBLE);
		else
			addIcon.setVisibility(View.GONE);
		deviceName.setText(mLeDevices.get(position).getName());
		macAddress.setText(mLeDevices.get(position).getAddress());
		return viewHolder.getConvertView();
	}

}
